package com.yxinsur.imc.web.controller.business;

import java.util.List;

import com.yxinsur.imc.business.domain.ImcTenant;
import com.yxinsur.imc.business.form.TenantForm;
import com.yxinsur.imc.business.form.TenantUserForm;
import com.yxinsur.imc.business.service.IImcTenantService;
import com.yxinsur.imc.business.vo.TenantVo;
import com.yxinsur.imc.common.constant.UserConstants;
import com.yxinsur.imc.common.core.domain.entity.SysOrg;
import com.yxinsur.imc.common.core.domain.entity.SysUser;
import com.yxinsur.imc.common.utils.StringUtils;
import com.yxinsur.imc.system.service.ISysOrgService;
import com.yxinsur.imc.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yxinsur.imc.common.annotation.Log;
import com.yxinsur.imc.common.core.controller.BaseController;
import com.yxinsur.imc.common.core.domain.AjaxResult;
import com.yxinsur.imc.common.enums.BusinessType;
import com.yxinsur.imc.common.core.page.TableDataInfo;

/**
 * 租户Controller
 *
 * @author imc
 * @date 2021-03-26
 */
@Api(value="租户信息管理",tags={"租户信息管理"})
@RestController
@RequestMapping("/business/tenant")
public class ImcTenantController extends BaseController
{
    @Autowired
    private IImcTenantService imcTenantService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysOrgService orgService;

    /**
     * 查询租户列表
     */
    //@PreAuthorize("@ss.hasPermi('system:tenant:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询租户列表")
    public TableDataInfo list(TenantForm tenantSaveForm)
    {
        startPage();
        List<TenantVo> list = imcTenantService.selectImcTenantList(tenantSaveForm);
        return getDataTable(list);
    }

    /**
     * 查询租户下的用户信息
     */
    @ApiOperation(value = "获取用户列表")
    @GetMapping("/list4tenantId")
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = imcTenantService.selectUserList(user);
        return getDataTable(list);
    }


    /**
     * 获取租户详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:tenant:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取租户详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(imcTenantService.selectImcTenantById(id));
    }

    /**
     * 新增租户
     */
    //@PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @ApiOperation("新增租户信息")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TenantForm tenantSaveForm)
    {
        //部门信息校验
        SysOrg sysOrg = new SysOrg();
        sysOrg.setOrgName(tenantSaveForm.getCompanyName());//一级部门信息
        if (UserConstants.NOT_UNIQUE.equals(orgService.checkOrgNameUnique(sysOrg)))
        {
            return AjaxResult.error("新增部门'" + sysOrg.getOrgName() + "'失败，部门名称已存在");
        }
        return toAjax(imcTenantService.insertImcTenant(tenantSaveForm));
    }


    /**
     * 新增租户
     */
    //@PreAuthorize("@ss.hasPermi('system:tenant:add')")
    @ApiOperation("新增租户管理员信息")
    @Log(title = "租户", businessType = BusinessType.INSERT)
    @PostMapping("/addTenantUser")
    public AjaxResult addTenantUser(@RequestBody TenantUserForm user)
    {
        //用户信息校验
        SysUser sysUser = new SysUser();
        sysUser.setPhonenumber(user.getPhonenumber());
        sysUser.setEmail(user.getEmail());
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName())))
        {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(sysUser)))
        {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail())
                && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(sysUser)))
        {
            return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        return toAjax(imcTenantService.insertImcTenantUser(user));
    }

    /**
     * 修改租户
     */
    @ApiOperation("修改租户信息")
    @Log(title = "租户", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TenantForm tenantForm)
    {
        return toAjax(imcTenantService.updateImcTenant(tenantForm));
    }

    /**
     * 删除租户
     */
    @PreAuthorize("@ss.hasPermi('system:tenant:remove')")
    @Log(title = "租户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(imcTenantService.deleteImcTenantByIds(ids));
    }
}
